package com.qding.park.adapter.report.controller;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.validation.Validator;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.validation.BindException;
import org.springframework.validation.FieldError;
import org.springframework.validation.ObjectError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.servlet.ModelAndView;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.FastJsonJsonView;
import com.qding.park.adapter.report.constants.FrontEndInterfaceConstant;
import com.qding.park.adapter.report.constants.WebConstants;
import com.qding.park.adapter.report.exception.ValidateException;
import com.qding.park.adapter.report.util.JsonObjectResp;
import com.qding.park.common.web.controller.BaseController;

public class WebController extends BaseController{
    
    protected static final Logger _log = LoggerFactory.getLogger(WebController.class);
    
    
    
    public  JsonObjectResp  buildSuccessResponse(){
        
        JsonObjectResp  response=new JsonObjectResp();
        response.setCode(FrontEndInterfaceConstant.RETURN_SUCCESS);
        response.setMsg("OK");
        return response;
        
    }
    
    
    
    /**
     * 绑定时间
     *
     * @param binder
     */
    @InitBinder
    protected void dateBinder(WebDataBinder binder) {
        // The date format to parse or output your dates
        SimpleDateFormat dateFormat = new SimpleDateFormat(WebConstants.TIME_FORMAT);
        // Create a new CustomDateEditor
        CustomDateEditor editor = new CustomDateEditor(dateFormat, true);
        // Register it as custom editor for the Date type
        binder.registerCustomEditor(Date.class, editor);
    }
    
    
    
    /*
     * Bind出错，这里是最高优先级的处理
     */
    @ExceptionHandler({BindException.class})
    public ModelAndView handleBindException(final BindException e) {
        JSONObject  jsonObj=new JSONObject();
          for (Object error : e.getAllErrors()) {
              
              if(error instanceof FieldError ){
                  FieldError fieldError=(FieldError) error;
                  String errorMsg=fieldError.getDefaultMessage();
                  String fieldName=fieldError.getField();
                  jsonObj.put(fieldName, errorMsg);
              }
             
          }
          
          ModelAndView mv = new ModelAndView();  
          FastJsonJsonView view = new FastJsonJsonView();  
          Map<String, Object> attributes = new HashMap<String, Object>();  
          Map<String, Object> data = new HashMap<String, Object>(); 
          data.put("field", jsonObj);
          attributes.put("code", FrontEndInterfaceConstant.RETURN_VERIFICATION_FAIL);  
          attributes.put("data", data);
          attributes.put("msg", "参数校验错误");
          view.setAttributesMap(attributes);  
          mv.setView(view);   
          return mv;
    }
    
    
    
    
    @ExceptionHandler({ValidateException.class})
    public ModelAndView handleValidateException(final ValidateException e) {
          
        JSONObject  jsonObj=new JSONObject();
        jsonObj.putAll(e.getFieldError());
        ModelAndView mv = new ModelAndView();  
        FastJsonJsonView view = new FastJsonJsonView();  
        Map<String, Object> attributes = new HashMap<String, Object>();  
        Map<String, Object> data = new HashMap<String, Object>(); 
        data.put("field", jsonObj);
        attributes.put("code", FrontEndInterfaceConstant.RETURN_VERIFICATION_FAIL);  
        attributes.put("data", data);
        attributes.put("msg", "参数校验错误["+e.getRemark()+"]");
        view.setAttributesMap(attributes);  
        mv.setView(view);   
        return mv;
    }
    /*
     * Bind出错，这里是最高优先级的处理
     */
    @ExceptionHandler({MethodArgumentNotValidException.class})
    public ModelAndView handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
        JSONObject  jsonObj=new JSONObject();
          for (Object error : e.getBindingResult().getAllErrors()) {
              
              if(error instanceof FieldError ){
                  FieldError fieldError=(FieldError) error;
                  String errorMsg=fieldError.getDefaultMessage();
                  String fieldName=fieldError.getField();
                  jsonObj.put(fieldName, errorMsg);
              }
             
          }
          
          ModelAndView mv = new ModelAndView();  
          FastJsonJsonView view = new FastJsonJsonView();  
          Map<String, Object> attributes = new HashMap<String, Object>();  
          Map<String, Object> data = new HashMap<String, Object>(); 
          data.put("field", jsonObj);
          attributes.put("code", FrontEndInterfaceConstant.RETURN_VERIFICATION_FAIL);  
          attributes.put("data", data);
          attributes.put("msg", "参数校验错误");
          view.setAttributesMap(attributes);  
          mv.setView(view);   
          return mv;
    }
    
    
    @ExceptionHandler({Exception.class})
    public ModelAndView handleException(final Exception e) {
          
        ModelAndView mv = new ModelAndView();  
        FastJsonJsonView view = new FastJsonJsonView();  
        Map<String, Object> attributes = new HashMap<String, Object>();  
        attributes.put("code", FrontEndInterfaceConstant.RETURN_SYSTEM_FAIL);  
        attributes.put("msg", e.getMessage());
        view.setAttributesMap(attributes);  
        mv.setView(view);   
        return mv;
    }

}
